package com.lid.labelview.slice;

import com.lid.labelview.ResourceTable;
import com.lid.lib.LabelImageView;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.global.resource.solidxml.TypedAttribute;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * ListViewActivity;
 *
 * @since 2021-04-13
 */
public class ListViewActivity extends AbilitySlice {
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "ListViewActivity");
    private static final int INT_4 = 4;
    private CategoryAdapter categoryAdapter;

    private ListContainer listView;

    private List<CategoryData> data = new ArrayList<>();

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);

        setUIContent(ResourceTable.Layout_activity_list_view);

        listView = (ListContainer) findComponentById(ResourceTable.Id_list_view);
        categoryAdapter = new CategoryAdapter(data, this);
        listView.setItemProvider(categoryAdapter);

        listView.scrollTo(0);

        if (data.size() == 0) {
            getCategoryData();
            getCategoryData();
            getCategoryData();
        }
    }

    /**
     * CategoryData;
     *
     * @since 2021-04-13
     */
    static class CategoryData {
        private String image;
        private String text;
    }

    private void getCategoryData() {
        {
            CategoryData item = new CategoryData();
            item.text = "Following the founding of New China, the policy of cultural reawakening offered "
                    + "Kunqu Opera the chance to emerge from a long period of neglect.";
            item.image = ResourceTable.Media_k1 + "";
            data.add(item);
        }

        {
            CategoryData item = new CategoryData();
            item.text = "Kunqu Opera can trace its origins back to the Ming Dynasty about six hundred "
                    + "years ago, and a small town south of the Yangtze, called Kunshan.";
            item.image = ResourceTable.Media_k2 + "";
            data.add(item); }

        {
            CategoryData item = new CategoryData();
            item.text = "The popularity of Kunqu Opera historically has a lot to do with the support it "
                    + "received from the imperial court, from the time of Emperor Kangxi, onwards.";
            item.image = ResourceTable.Media_k3 + "";
            data.add(item); }

        {
            CategoryData item = new CategoryData();
            item.text = "Perhaps the greatest masterpiece of Kunqu Opera is “The Peony Pavilion” written "
                    + "by Tang Xianzu in the early years of the 17th century.";
            item.image = ResourceTable.Media_k4 + "";
            data.add(item); }

        {
            CategoryData item = new CategoryData();
            item.text = "Kunqu Opera is remarkable, if for nothing else, because of the long time it has"
                    + " been around. It continues to exert a strong appeal today.";
            item.image = ResourceTable.Media_k5 + "";
            data.add(item); }

        {
            CategoryData item = new CategoryData();
            item.text = "In the late Qing Dynasty, after five hundred years of development, Kunqu faced"
                    + " the greatest crisis in its existence.";
            item.image = ResourceTable.Media_k6 + "";
            data.add(item); }

        {
            CategoryData item = new CategoryData();
            item.text = "During the Qing Dynasty that followed, it became so popular that it was said to "
                    + "have an influence at every level of society, from the imperial court, down.";
            item.image = ResourceTable.Media_k7 + "";
            data.add(item); }

        {
            CategoryData item = new CategoryData();
            item.text = "The Qing Dynasty’s greatest playwrights, are Hong Sheng and Kong Shangren,"
                    + " who wrote, respectively, “The Palace of Eternal Youth” and “The Peach Blossom Fan”.";
            item.image = ResourceTable.Media_k8 + "";
            data.add(item); }

        {
            CategoryData item = new CategoryData();
            item.text = "Every period of history has its own fashions and tastes in clothing, music, etc."
                    + " During the Ming Dynasty, it was fashionable among the intelligentsia to enjoy Kunqu Opera.";
            item.image = ResourceTable.Media_k9 + "";
            data.add(item); }

        {
            CategoryData item = new CategoryData();
            item.text = "Kunqu Opera can trace its origins back to the "
                    + "late Ming Dynasty and a small town south of the Yangtze, called Kunshan.";
            item.image = ResourceTable.Media_k10 + "";
            data.add(item); }

        categoryAdapter.notifyDataChanged();
    }

    /**
     * CategoryAdapter
     *
     * @since 2021-04-13
     */
    static class CategoryAdapter extends BaseItemProvider {
        private List<CategoryData> categoryDataList;
        private AbilitySlice slice;

        /**
         * CategoryAdapter
         *
         * @param categoryAdapterList
         * @param slice
         */
        public CategoryAdapter(List<CategoryData> categoryAdapterList, AbilitySlice slice) {
            this.categoryDataList = categoryAdapterList;
            this.slice = slice;
        }

        @Override
        public int getCount() {
            return categoryDataList == null ? 0 : categoryDataList.size();
        }

        @Override
        public Object getItem(int i) {
            if (categoryDataList != null && i >= 0 && i < categoryDataList.size()) {
                return categoryDataList.get(i);
            }
            return Collections.emptyList();
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
            final Component cpt;
            if (convertComponent == null) {
                cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_list_view_item, null, false);
            } else {
                cpt = convertComponent;
            }
            CategoryData categoryData = categoryDataList.get(position);

            Text text = (Text) cpt.findComponentById(ResourceTable.Id_text);
            text.setText(categoryData.text);

            LabelImageView labelImageView = (LabelImageView) cpt.findComponentById(ResourceTable.Id_image);
            labelImageView.setPixelMap(Integer.parseInt(categoryData.image));
            labelImageView.setLabelText("LID " + position);
            labelImageView.setLabelTextStyle(TypedAttribute.COLOR_ATTR);

            Text line = (Text) cpt.findComponentById(ResourceTable.Id_line);
            line.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
            line.setHeight(INT_4);

            return cpt;
        }
    }
}
